package DP;

/**
 * @author 曹见朋
 * @create 2024-06-16-21:58
 */
public class Main53 {
    // TODO 53. 最大子数组和

    public static int maxSubArray(int[] nums) {
        int n = nums.length;
        if(n == 1) return nums[0];
        int[] dp = new int[n];
        int result = 0;
        dp[0] = nums[0];

        for(int i = 1; i < n; i++){
            dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
            result = Math.max(result, dp[i]);
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{-2,1,-3,4,-1,2,1,-5,4};
        System.out.println(maxSubArray(nums));
    }
}
